import Vue       from 'vue'
import App       from './App'
import uniFly    from 'unifly';
import request   from './common/request.js'
import utils     from './common/utils.js'
import md5       from './common/md5.js'
import Big       from './common/big.js'
import $api_base from './api/api_base.js'

Vue.config.productionTip = false
Vue.prototype.$request = request

Vue.prototype.reg = function(fn) {
    var self = this;
    var reg_from = ''
     // #ifdef MP-WEIXIN
    	reg_from = 'wmp';
    // #endif
    // #ifdef MP-ALIPAY
    	reg_from = 'amp';
    // #endif
    $api_base.post($api_base.reg,{reg_from:reg_from}).then(function(res) {
        console.log(res);
        let ret = res.data;
        if (ret.ok != 1) {
            uni.showModal({
                content: res.msg, success: function (res) {
                    self.clear();
                    uni.navigateBack();
                }
            });
        } else {
            fn();
        }
    });
}

Vue.prototype.clear = function () {
        uni.clearStorageSync()
    },
    
Vue.prototype.loginReg = function() {
		var self = this;
        return new Promise(function (resolve, reject) {
            uni.getProvider({
                service: 'oauth',
                success: function (res) {
                    console.log(res.provider)
                    let provider = res.provider;
                        uni.login({
                            provider: provider,
                            success: function (res) {
                                uni.setStorageSync("code", res.code);
                                // #ifdef MP-WEIXIN
                                    var openid = uni.getStorageSync('openid');  //获取本地缓存
                                    var params = {code:res.code, openid: openid, reg_from:'wmp'};	
                                // #endif
                                // #ifdef MP-ALIPAY
                                    var openid = '';
                                    var params = {code:res.code, openid: openid, reg_from:'amp'};
                                // #endif
                                $api_base.post($api_base.init,params).then(function(res) {
                                        var header = res.headers;
                                        var data = res.data;
                                        if (data.ok == 1) {
                                            var session = utils.getCookie(header, 'PHPSESSID');
                                            if (session.length > 0) {
                                                uni.setStorageSync('session', 'PHPSESSID=' + session)
                                            }
                                            if (!openid) { 
                                                uni.setStorageSync('openid',data.openid); 
                                            }
                                            var result = function () {
                                                if (openid !== '') {
                                                    resolve();
                                                } else {  
                                                    reject('error');  
                                                }
                                            };
                                            if (data.isReg == 0) {
                                                self.reg(function () {
                                                    result();
                                                });
                                            } else {
                                                result();
                                            }
                                        } else {
                                           reject('error');
                                        }
                                  }).catch(function(error) {
                                        uni.setStorageSync('noNetwork',true); 
                                        reject('error');
                                  })
                            }
                        });
                }
            });
            
        }).catch(function(reason){
			console.log('catch:', reason);
		});
    }


App.mpType = 'app'

const app = new Vue({
	...App,
})
app.$mount()


	